#include <stdio.h>
#include <stdlib.h>

#include "Log.h"
#include "fileutils.h"
#include "media/connect/MediaConnect.h"
#include "media/video/VideoRecorder.h"
#include "func_storage.h"
#include "dvr_svr.h"
#include "http_svr.h"

HttpService g_httpserver;
// ------------------------------------------------------

#define DEMO_LOG_DIR   "/tmp/demo/log"

static void buildLogControllParams(ControllParams *params, const char *logDir){
    memset(params->logDir, 0, sizeof(params->logDir));
    snprintf(params->logDir, sizeof(params->logDir), "%s", logDir);
    memset(params->logFileName, 0, sizeof(params->logFileName));
    strcpy(params->logFileName, "VideoRecordDemo.log");
    memset(params->developMode, 0, sizeof(params->developMode));
    strcpy(params->developMode, DEVELOP_MODE_DEBUG);
    params->outputToFile = 1;
    params->maxFileSize = MAX_FILE_SIZE;
    params->maxFileCount = MAX_FILE_COUNT;
}

static void startLogMoudle(){
    printf("App log dir = %s\n", DEMO_LOG_DIR);

    ControllParams params;
    buildLogControllParams(&params, DEMO_LOG_DIR);
    if (opendir(DEMO_LOG_DIR)){
        printf("App log dir already exist\n");
        initLogMoudle(&params);
        return;
    }
    int isCreate = fs_mkdir(DEMO_LOG_DIR, true);
    if(isCreate == 0)
        printf("create dir\n");
    else
        printf("log dir create failed! error code = %d \n", isCreate);

    initLogMoudle(&params);
}

// ------------------------------------------------------
int main(int argc, char **argv) {
    printf("@@ enter main! @@ \n");

    startLogMoudle();
    int count = 0, ret = 0, readLen = 0;

    sos_NwInit(NULL);
    nw_log_warn("after network init!");
    usleep(1*1000*1000);
    media_load_connect_moudle();
    video_record_load_moudle();

    int stream_nums = 1;
    start_storage(stream_nums,"/dev/mmcblk0");
    start_dvr_svr();
    if(-1 == access("./www",0))
    {
        system("mkdir -p /customer/www");    
    }
    
    system("ln -s /tmp/record /customer/www/record");
    //system("ln -s /tmp/tfcommon /customer/www/common");
    g_httpserver.start("80");

    int runTime = 0;
    while(1) {
        usleep(20000);
        runTime += 20;
        if(0 == (runTime % 30000)){
            printf("after 30s: I still live!");
        }
        if(runTime >= 43200000){
            //12h重置
            printf("application runTime be reset!");
            runTime = 0;
        }
    }

    return 0;
}


